package com.yzf.smsforward;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import com.yzf.pcsmsforward.R;
import com.yzf.smsforward.utils.Utils;

/**
 * 项目名称：SMSForward
 * 类描述：短信筛选字段弹窗
 * 创建人：luohao
 * 创建时间：2021/3/2 15:37
 * 修改人：luohao
 * 修改时间：2021/3/2 15:37
 * 修改备注：
 */
public class MatchingRulesDialog extends Dialog {
    private  Context mContext;
    private  Handler hander;
    private  MatchingRules matchingRules;
    private ImageView mIV_dialog_close;
    private EditText mET_matching_rules;
    private Button mBT_save_matching_rules;

    public MatchingRulesDialog(@NonNull Context context, Handler hander, MatchingRules matchingRules) {
        super(context);
        this.mContext = context;
        this.hander = hander;
        this.matchingRules = matchingRules;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.matching_rules_dialog_layout);
        initView();
    }

    private void initView() {
        mIV_dialog_close = this.findViewById(R.id.mIV_dialog_close);
        mET_matching_rules = this.findViewById(R.id.mET_matching_rules);
        mBT_save_matching_rules = this.findViewById(R.id.mBT_save_matching_rules);
        mET_matching_rules.setText(Constants.MATCHING_RULES_STRING);
        mET_matching_rules.setSelection(Constants.MATCHING_RULES_STRING.length());
        mIV_dialog_close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                MatchingRulesDialog.this.dismiss();
            }
        });
        mBT_save_matching_rules.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String s = mET_matching_rules.getText().toString().trim();
                if (!TextUtils.isEmpty(s)) {
                    String s1 = String.valueOf(s.charAt(0));
                    if (s1.equals("/")) {
                        s = s.substring(1, s.length() - 1);
                    }
                    s1 = String.valueOf(s.charAt(s.length() - 1));
                    if (s1.equals("/")) {
                        s = s.substring(0, s.length() - 1);
                    }
                }
                Utils.save2SP(mContext, Constants.MATCHING_RULES, Constants.MATCHING_RULES, s);
                Constants.matching_rules_array = s.split("/");
                matchingRules.backMatchingRules(s);
                MatchingRulesDialog.this.dismiss();
//                hander.sendEmptyMessage(0);
            }
        });

    }

    public interface MatchingRules {
        void backMatchingRules(String matchingRules);
    }
}
